package com.zxx.admin.controller.base;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zxx.admin.dto.code.CodeStudyDTO;
import com.zxx.admin.entity.base.CodeStudyEntity;
import com.zxx.admin.service.base.CodeStudyService;
import com.baomidou.mybatisplus.extension.api.R;
import com.zxx.admin.utils.SpliceUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * @Description: 代码管理 控制层
 * @author: zhuangxx
 * @date 2020-07-13
 */
@RestController
@RequestMapping("study/code")
@ApiModel(value="代码管理", description="代码管理")
@Api(description = "代码管理")
public class CodeStudyController  {

    /**
     * CodeStudy  服务
     */
    @Autowired
    private CodeStudyService codeStudyService;


    /**
     * @Description CodeStudy列表查询
     * @Author zhuangxx
     * @Date 2020-3-26
     * @return CodeStudy列表集合
     */
    @GetMapping(value = "listNoPage")
    @ApiOperation(value = "返回CodeStudy列表集合", notes = "返回CodeStudy列表集合")
    public R<List> listNoPage() {
        return R.ok(codeStudyService.list());
    }

    /**
     * @Description CodeStudy分页查询
     * @Author zhuangxx
     * @Date 2020-3-26
     * @param filters 过滤条件
     * @param sorts 排序规则
     * @param size 分页大小
     * @param page 页码
     * @return CodeStudy分页集合
     */
    @GetMapping(value = "/listPage")
    @ApiOperation(value = "返回CodeStudy分页集合", notes = "返回CodeStudy分页集合")
    public R<IPage> listPage(
            @RequestParam(value = "filters", required = false) String filters,
            @ApiParam(name = "sorts", value = "排序，规则参见说明文档", defaultValue = "")
            @RequestParam(value = "sorts", required = false) String sorts,
            @ApiParam(name = "size", value = "分页大小", defaultValue = "15")
            @RequestParam(value = "size", required = false) int size,
            @ApiParam(name = "page", value = "页码", defaultValue = "1")
            @RequestParam(value = "page", required = false) int page) {
        QueryWrapper<CodeStudyEntity> ew = new QueryWrapper();
        SpliceUtil.search(filters, sorts, ew);
        Page<CodeStudyEntity> page1 = new Page(page, size);
        IPage result = codeStudyService.page(page1, ew);
        return R.ok(result);
    }

    /**
     * @Description 新增CodeStudy
     * @Author zhuangxx
     * @Date 2020-3-26
     * @param codeStudyDTO
     * @return 新增结果
     */
    @PostMapping
    @ApiOperation(value = "新增CodeStudy", notes = "新增CodeStudy")
    public R<Boolean> create(
            @ApiParam(name = "demoEntity", value = "", defaultValue = "")
            @RequestBody @Validated CodeStudyDTO codeStudyDTO) {
        return R.ok(codeStudyService.addCode(codeStudyDTO));
    }

    /**
     * @Description 编辑CodeStudy
     * @Author zhuangxx
     * @Date 2020-3-26
     * @param codeStudyDTO CodeStudy实体
     * @return 编辑结果
     */
    @PutMapping
    @ApiOperation(value = "编辑CodeStudy", notes = "编辑CodeStudy")
    public R<Boolean> update(
            @ApiParam(name = "demoEntity", value = "", defaultValue = "")
            @RequestBody CodeStudyDTO codeStudyDTO) {
        return R.ok(codeStudyService.updateCode(codeStudyDTO));
    }

    /**
     * @Description 删除CodeStudy
     * @Author zhuangxx
     * @Date 2020-3-26
     * @param id 待删除CodeStudyid
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除CodeStudy", notes = "删除CodeStudy")
    public R<Boolean> delete(
            @ApiParam(name = "id", value = "id", defaultValue = "")
            @PathVariable Integer id) {
        return R.ok(codeStudyService.delCode(id));
    }
}
